package com.lianxian.xindong.life.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.entities.FoodRandkingFilterOneRecyclerModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FoodRankingFilterOneAdapter extends RecyclerView.Adapter {
    public int currentPosition;
    Context context;

    List<FoodRandkingFilterOneRecyclerModel> list;

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public List<FoodRandkingFilterOneRecyclerModel> getList() {
        return list;
    }

    public void setList(List<FoodRandkingFilterOneRecyclerModel> list) {
        this.list = list;
    }

    public FoodRankingFilterOneAdapter(List<FoodRandkingFilterOneRecyclerModel> list,Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_food_ranking_filter_one, parent, false);
        FoodRankingFilterOneAdapter.ViewHolder viewHolder = new FoodRankingFilterOneAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        FoodRandkingFilterOneRecyclerModel filterOneRecyclerModel = list.get(position);
        FoodRankingFilterOneAdapter.ViewHolder viewHolder = (FoodRankingFilterOneAdapter.ViewHolder) holder;
        viewHolder.nameTV.setText(filterOneRecyclerModel.getName());
        viewHolder.nameTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPosition = position;
                notifyDataSetChanged();
            }
        });
        if (currentPosition == position) {
            viewHolder.picTV.setVisibility(View.VISIBLE);
            viewHolder.nameLayout.setBackgroundResource(R.drawable.filter_one_backgroud);
        }else{
            viewHolder.picTV.setVisibility(View.GONE);
            viewHolder.nameLayout.setBackgroundResource(R.color.transparent);

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.filter_one_name)
        TextView nameTV;
        @BindView(R2.id.filter_one_pic)
        ImageView picTV;
        @BindView(R2.id.name_layout)
        LinearLayout nameLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
